<?php

namespace App\Http\Controllers\background;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use App\Models\Mysql\{XnGoodsCarmyType, XnMechantCarmi, XnSupplierGoods};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class LinkCarmiController extends Controller
{
    public function carmiinsert(Request $request){
        $params = $request -> post();
        if(!$params){
            return fails('请传参');
        }
        $goods_id = $params['goods_id'];
        $c_id = $params['c_id'];
        if(empty($params['goods_id'])){
            return fails('请传参');
        }
        $c_c_id = $params['c_c_id'] ?? 0;
        $c_b_id = $params['c_b_id'] ?? 0;
        if(!empty($c_id)){
            XnSupplierGoods::where('id',$params['goods_id']) -> update([
                'c_id'=>$c_id,
                'c_c_id'=>$c_c_id,
                'c_b_id'=>$c_b_id
            ]);
        }else{
            $c_info = XnSupplierGoods::where('id',$params['goods_id']) -> select('c_id','c_c_id','c_b_id') -> first();
            $c_id = $c_info -> c_id;
            $c_c_id = $c_info -> c_c_id;
            $c_b_id = $c_info -> c_b_id;
        }
        $time = time();
        $linkinfoarr = explode(',',$params['kamiinfos']);
        $linkinfoarr = array_unique($linkinfoarr);
        $arr = [];
        $datacounts['data'] = [];
        foreach($linkinfoarr as $k => $v){
            $where[] = ['goods_id','=',$params['goods_id']];
            $where[] = ['carmi','=',$v];
            $count = XnMechantCarmi:: where($where) -> exists();
            if($count){
                $datacounts['data'][] = $v;
                XnMechantCarmi::where($where) -> update(['u_time'=>$time]);
                unset($linkinfoarr[$k]);
                continue;
            }
            $arr_arr = [
                'goods_id' => $goods_id,
                'carmi' => $v,
                'first' =>$params['first'],
                'is_use' => 2,
                'time' => $time,
                'use_time' => 0,
                'account' => '',
                'orderNum' => '',
                'url' =>$params['url'],
                'peoples' => $params['peoples'],
                'o_type'=>$params['o_type'],
                'c_id' => $c_id,
                'c_b_id'=>$c_b_id,
                'c_c_id'=>$c_c_id
            ];
            if(isset($params['mechant_number'])){
                $arr_arr['mechant_num'] = $params['mechant_number'];
            }
            $arr[] = $arr_arr;
        }
        Db::connection(request()->get('db', 'supplier'))->beginTransaction();
        try{
            $res = XnMechantCarmi:: insert($arr);
            if(!$res){
                $res = 0;
            }
            $supplier_status = XnGoodsCarmyType::find($c_id) -> type;
            $goods_model = XnSupplierGoods::find($goods_id);
            $goods_model -> goods_belong = $supplier_status;
            $goods_model -> save();
            DB::connection(request()->get('db', 'supplier'))->commit();
            $datacounts['count'] = $res;
            return successly($datacounts);
        }catch(\Exception $e){
            DB::connection(request()->get('db', 'supplier'))->rollBack();
           var_dump($e -> getMessage());
            return fails('添加失败');
        }
    }

    public function elsenum(Request $request)
    {
        $goods_id = $request->post('goods_id');
        if (empty($goods_id)) {
            return fails('请传参');
        }
        $where[] = ['goods_id', '=', $goods_id];
        $url = XnMechantCarmi:: where($where)->orderBy('time', 'desc')->select('url', 'c_id', 'c_b_id', 'c_c_id')->first();
        $where[] = ['is_use', '=', 2];
        $count = XnMechantCarmi:: where($where)->count();
        if (!$url || $count <= 0) {
            return fails('暂未查到数据');
        }
        $url = $url -> toArray();
        $res = [
            'count' => $count,
            'url' => $url['url'],
            'c_id' => $url['c_id'],
            'c_b_id' => $url['c_id'],
            'c_c_id' => $url['c_c_id']
        ];
        return successly($res);
    }

    public function carmilinklist(Request $request){
        $params = $request -> post();
        $condition = [];
        if($params['status']){
            $condition[] = ['is_use','=',$params['status']];
        }
        if ($params['start_time'] != 0 && $params['end_time'] != 0) {
            $condition[] = ['time','>=',strtotime($params['start_time'])];
            $condition[] = ['time','<=',strtotime($params['end_time'])];

        }
        $start_utime = isset($params['start_utime']) ? $params['start_utime'] : 0;//开始使用时间
        $end_utime = isset($params['end_utime']) ? $params['end_utime'] : 0;//结束使用时间
        if ($start_utime != 0 && $end_utime != 0) {
            $condition[] = ['use_time','>=',strtotime($start_utime)];
            $condition[] = ['use_time','<=',strtotime($end_utime)];
        }
        if($params['account']){
            $condition[] = ['account','=',$params['account']];
        }
        if($params['carmi']){
            $condition[] = ['carmi','=',$params['carmi']];
        }


        $condition['goods_id'] = $params['goods_id'];
        $res = XnMechantCarmi::where($condition)
            ->orderBy('id','desc')
            ->paginate(10)
            -> toArray();
        if($res){
            foreach($res['data'] as $k=>$v){
                $res['data'][$k]['time'] = date('Y-m-d H:i:s',$v['time']);
                if($v['use_time'] == 0){
                    $res['data'][$k]['use_time'] = '';
                }else{
                    $res['data'][$k]['use_time'] = date('Y-m-d H:i:s',$v['use_time']);
                }
            }
            return successly($res);
        }else{
            return fails('未查询到数据');
        }
    }

    public function carmilinkdel(Request $request){
        $ids = $request -> post('ids');
        if(empty($ids)){
            return fails('请传参');
        }
        $del = XnMechantCarmi:: find($ids);
        $del -> delete();
        return successly();

    }

    public function carmilinkdelmany(Request $request){
        $ids = $request -> post('ids');
        if(empty($ids)){
            return fails('请传参');
        }
        $carmi_arr = explode(',',$ids);
        XnMechantCarmi::whereIn('id',$carmi_arr)-> delete();
        return successly();
    }

    public function kamitopsals(Request $request){
            $ids = $request -> post('ids');
            if(empty($ids)){
                return fails('请传参');
            }
            $carmi_arr = explode(',',$ids);
            $res = XnMechantCarmi:: whereIn('id',$carmi_arr)
                -> where('first','<>',1)
                -> update(['first' => 1]);
            if($res){
                return successly();
            }else{
                return  fails('设置失败');
            }
    }

    public function emptycarmi(Request $request){
        $goods_id = $request -> post('goods_id');
        if(empty($goods_id)){
            return  fails('请传参');
        }
        $res = XnMechantCarmi:: where('goods_id',$goods_id) -> delete();
        if($res){
            return successly();
        }else{
            return fails('删除失败');
        }
    }

    public function usemany(Request $request){
        $goods_id = $request-> post('kamiids');
        if(empty($goods_id)){
            return fails('请传参');
        }
        $goods_id = explode(',',$goods_id);
        $count = XnMechantCarmi:: whereIn('id',$goods_id) -> update(['is_use'=>1,'use_time'=>time()]);
        if($count){
            return successly();
        }else{
            return fails('修改失败');
        }

    }

    public function repeatcarmi(Request $request){
        $goods_id = $request -> post('goods_id');
        $start_time = $request -> post('s_time');
        $end_time = $request -> post('u_time');
        $carmi = $request -> post('carmi');
        if(empty($goods_id)){
            return fails('请传参');
        }
        if(!empty($start_time) && !empty($end_time)){
            $condition[] = ['u_time','>=',strtotime($start_time)];
            $condition[] = ['u_time','<=',strtotime($end_time)];
        }else{
            $condition[] = ['u_time','<>',0];
        }
        if(!empty($carmi)){
            $condition[] = ['carmi','=',$carmi];
        }
        $condition[] = ['goods_id','=',$goods_id];
        $res = XnMechantCarmi:: where($condition)
            -> select('carmi','u_time')
            -> orderBy('u_time','desc')
            -> paginate(10)
            -> toArray();
        if($res){
            foreach($res['data'] as $k=>$v){
                $res['data'][$k]['use_time'] = date('Y-m-d H:i:s',$v['u_time']);
            }
            return successly($res);
        }else{
            return fails('获取失败');
        }
    }

    public function abnormalcarmi(Request $request){
        $params = $request->post();
        $start_time = strtotime($params['s_time']);
        $end_time = strtotime($params['u_time']);
        if(!empty($start_time) && !empty($end_time)) {
            $condition[] = ['use_time', '>=', strtotime($start_time)];
            $condition[] = ['use_time', '<=', strtotime($end_time)];
        }
        if(!empty($params['carmi'])){
            $condition[] = ['carmi','=',$params['carmi']];
        }
        $condition[] = ['goods_id','=',$params['goods_id']];
        $res = XnMechantCarmi:: where($condition)
            -> whereIn('rechargeStatus',[2,3])
            -> select('account','carmi','use_time','errMessage')
            -> orderBy('u_time','desc')
            -> paginate(10)
            -> toArray();
        if($res){
            foreach ($res['data'] as $k=> $v){
                $res['data'][$k]['use_time'] = date('Y-m-d H:i:s',$v['use_time']);
                $res['data'][$k]['goodsName'] = XnSupplierGoods::where('id',$params['goods_id'])->value('goods_name');
            }
            return successly($res);
        }else{
            return fails('获取失败');
        }
    }
}
